package com.huanbao.nongwu.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huanbao.common.pojo.nwrabc.*;
import com.huanbao.common.pojo.nwrabc.vo.NwUserDetails;
import com.huanbao.nongwu.dao.*;
import com.huanbao.nongwu.pojo.beans.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/* ━━━━━━佛祖保佑━━━━━━
 *                  ,;,,;
 *                ,;;'(    社
 *      __      ,;;' ' \   会
 *   /'  '\'~~'~' \ /'\.)  主
 * ,;(      )    /  |.     义
 *,;' \    /-.,,(   ) \    码
 *     ) /       ) / )|    农
 *     ||        ||  \)
 *     (_\       (_\
 * ━━━━━━永无BUG━━━━━━
 * @author ：zjq
 * @date ：2021/3/22 15:18
 * @description: TODO
 * @version: V1.0
 * @slogan: 天下风云出我辈，一入代码岁月催
 */
@Service
public class AdminServiceImpl {


    @Autowired
    private SysUserDao sysUserDao;

    @Autowired
    private SysMenuDao sysMenuDao;

    @Autowired
    private SysUserRoleDao sysUserRoleDao;

    @Autowired
    private SysRoleMenuDao sysRoleMenuDao;

    @Autowired
    private SysPermissionDao sysPermissionDao;

    @Autowired
    private SysRolePermissionDao sysRolePermissionDao;

    @Autowired
    private AdminDao adminDao;

    /**
     * 查 用户 以及 菜单
     *
     * @param loginName
     * @return
     */
    public NwUserDetails getUserInfoByName(String loginName) {

        NwUserDetails nwUserDetails = new NwUserDetails();


        // 1. 查用户
        QueryWrapper<SysUser> emp = new QueryWrapper<>();
        emp.eq("loginname", loginName);
        SysUser sysUser = sysUserDao.selectOne(emp);

        nwUserDetails.setSysUser(sysUser);

        if (null != sysUser) {
            // 查 角色
            QueryWrapper<SysUserRole> empRole = new QueryWrapper<>();
            empRole.eq("userid", sysUser.getId());
            SysUserRole sysUserRole = sysUserRoleDao.selectOne(empRole);

            // 查 菜单id

            if (null != sysUserRole) {
                QueryWrapper<SysRoleMenu> empRM = new QueryWrapper<>();
                empRM.eq("roleid", sysUserRole.getRoleid());
                List<SysRoleMenu> sysRoleMenus = sysRoleMenuDao.selectList(empRM);

                if (null != sysRoleMenus && sysRoleMenus.size() > 0) {

                    List<Integer> list = new ArrayList<>();
                    sysRoleMenus.stream().forEach(n -> {
                        list.add(n.getMenuid());
                    });

                    // 查菜单
                    QueryWrapper<SysMenu> empM = new QueryWrapper<>();
                    empM.in("id", list);
                    List<SysMenu> sysMenus = sysMenuDao.selectList(empM);

                    nwUserDetails.setSysMenus(sysMenus);

                    // 查权限
                    QueryWrapper<SysRolePermission> empRP = new QueryWrapper<>();
                    empRP.eq("roleid", sysUserRole.getRoleid());
                    List<SysRolePermission> sysRolePermissions = sysRolePermissionDao.selectList(empRP);

                    List<Integer> permissionList = new ArrayList<>();
                    sysRolePermissions.stream().forEach(n -> {
                        permissionList.add(n.getPermissionid());
                    });
                    QueryWrapper<SysPermission> empP = new QueryWrapper<>();
                    empP.in("id", permissionList);
                    List<SysPermission> sysPermissions = sysPermissionDao.selectList(empP);
                    nwUserDetails.setSysPermissions(sysPermissions);

                }

            }
        }


        return nwUserDetails;

    }


    public List<Admin> getAllAdmins() {
        return adminDao.selectList(null);
    }


}
